package com.burry.servlet;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadBase;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FilenameUtils;

import applelew.util.Debug;

import com.burry.ajax.upload.MonitoredDiskFileItemFactory;
import com.burry.ajax.upload.UploadListener;

public class FileUploadServlet extends HttpServlet {

	public FileUploadServlet() {
		super();
	}

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");

		// upload file
		List fileNameList = upload(request, response);
		String cid = request.getParameter("cid");
		String cname = request.getParameter("cname");
		String gid = request.getParameter("gid");

		String target = request.getParameter("target");
		request.setAttribute("fileNameMap", fileNameList);
		Debug.logInfo("Context path ==> " + request.getContextPath());
		request.getRequestDispatcher(
				"/manage/" + target + ".jsp?cid=" + cid + "&cname=" + cname
						+ "&gid=" + gid).forward(request, response);

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occure
	 */
	public void init() throws ServletException {
		// Put your code here
	}

	private List upload(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		// 用于存放上传文件的目录

		List fileNameList = new ArrayList();
		String uploadPath = request.getSession().getServletContext().getRealPath("/");

		// 创建了一个监听器，用来监听文件上传的状态
		// 进度条是通过这个监听器来动态改变的
		UploadListener listener = new UploadListener(request, 30);

		// (1)Create a factory for disk-based file items
		FileItemFactory factory = new MonitoredDiskFileItemFactory(listener);

		// (2)Create a new file upload handler
		ServletFileUpload upload = new ServletFileUpload(factory);

		try { // 允许上传的文件大小,4G
		// upload.setSizeMax(4 * 1024 * 1024 * 1024);

			// (3) process uploads ..
			List uploadlist = upload.parseRequest(request);
			// 开始读取上传信息
			Iterator iter = uploadlist.iterator();
			while (iter.hasNext()) {
				FileItem item = (FileItem) iter.next();
				if (!item.isFormField()) {
					// 忽略其他不是文件域的所有表单信息

					// 取到客户端完整 路径+文件名 c:/abc.jpg
					String fullpath = item.getName();

					// 取到 文件名 abc.jpg
					String filename = FilenameUtils.getName(fullpath);

					// 取到文件类型 jpg
					String filetype = getFiletype(filename);

					// 判断是否有非法的文件类型
					boolean flag = false;

					// 过滤掉的文件类型
					String[] errorType = { "exe", "com", "cgi", "jsp" };

					for (int i = 0; i <= errorType.length - 1; i++) {
						if (filetype.equals(errorType[i])) {
							flag = true;
						}
					}
					if (flag)
						// 如果有非法文件,退出
						break;

					if (!filename.equals("")) {
						// 文件重命名
						File path = new File(uploadPath);
						if (!path.exists()) {
							// 目录不存在则创建
							path.mkdir();
						}

						String dbPath = System.currentTimeMillis() + filename;
						String savepath = uploadPath + "//resource//products//" + dbPath;

						fileNameList.add(dbPath);
						// System.out.println("savepath is " + savepath);
						File saveFilepath = new File(savepath);

						item.write(saveFilepath);
					} else {
						// System.out.print("没东西!");
					}
				}
			}
		} catch (FileUploadBase.SizeLimitExceededException e) {
			// System.out.println("文件太大了吧!");
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return fileNameList;
	}

	// 取文件名后缀
	private String getFiletype(String fileName) {
		String type = "";
		if (fileName == null || fileName.equals(""))
			return type;
		int position = fileName.lastIndexOf(".");
		if (position != -1) {
			type = fileName.substring(position + 1, fileName.length());
		}
		return type;
	}

}
